import type { VNode } from 'vue';

import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { getDictOptions } from '#/utils/dict';
import { renderBrowserIcon, renderDict, renderOsIcon } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'ipaddr',
    label: 'IP地址',
  },
  {
    component: 'Input',
    fieldName: 'userName',
    label: '登录账号',
  },
  {
    component: 'Select',
    componentProps: {
      optionFilterProp: 'label',
      options: getDictOptions(DictEnum.SYS_COMMON_STATUS),
    },
    fieldName: 'status',
    label: '登录状态',
  },
  {
    component: 'RangePicker',
    fieldName: 'dateTime',
    label: '登录日期',
  },
];

export const columns: VxeGridProps['columns'] = [
  {
    align: 'center',
    type: 'checkbox',
    width: 45,
    resizable: false,
  },
  {
    title: '登录账号',
    field: 'userName',
    minWidth: 120,
    width: 120,
  },
  {
    title: '授权端口',
    field: 'deviceType',
    width: 120,
    minWidth: 120,
    slots: {
      default: ({ row }) => {
        return renderDict(row.deviceType, DictEnum.SYS_DEVICE_TYPE);
      },
    },
  },
  {
    title: 'IP地址',
    field: 'ipaddr',
    align: 'center',
    minWidth: 120,
    width: 120,
  },
  {
    title: '登录地址',
    field: 'loginLocation',
    minWidth: 200,
    width: 200,
  },
  {
    title: '操作系统',
    field: 'os',
    minWidth: 300,
    slots: {
      default: ({ row }) => {
        /**
         *  Windows 10 or Windows Server 2016 太长了 分割一下 详情依旧能看到详细的
         */
        const value = row.os;
        // if (value) {
        //   const split = value.split(' or ');
        //   if (split.length === 2) {
        //     value = split[0];
        //   }
        // }
        return renderOsIcon(value, false) as VNode;
      },
    },
  },
  {
    title: '浏览器',
    field: 'browser',
    width: 120,
    minWidth: 120,
    slots: {
      default: ({ row }) => {
        return renderBrowserIcon(row.browser, false) as VNode;
      },
    },
  },

  {
    title: '状态',
    field: 'status',
    align: 'center',
    minWidth: 80,
    width: 80,
    slots: {
      default: ({ row }) => {
        return renderDict(row.status, DictEnum.SYS_COMMON_STATUS);
      },
    },
  },
  // {
  //   title: '信息',
  //   field: 'msg',
  // },
  {
    title: '日期',
    field: 'loginTime',
    align: 'center',
    minWidth: 160,
    width: 160,
  },
  {
    field: 'action',
    fixed: 'right',
    align: 'center',
    slots: { default: 'action' },
    resizable: false,
    title: '操作',
    width: 'auto',
  },
];
